<?php
//Data loading
//By Bayu
$pdf=new COAPDFReport("P","mm","Letter");
$pdf->DBConnection($DBConnection->dbc);
$pdf->Period(GetParam("y",""),GetParam("m",""),GetParam("ly",""),GetParam("lm",""));
$pdf->AddPage();
$pdf->GenerateReport();
$pdf->Output();

class COAPDFReport extends PDF {
  /*function COAPDFReport($Page,$Size,$PageMode) {
    parent::PDF($Page,$Size,$PageMode);
    $this->ColSize = array(85,30,30,30,30);
    $this->TableName = "tblcoa";
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
  }*/

  function Header() {
	  $this->ColSize = array(85,30,30,30,30);
	  $this->AddFont('arialn','','arialn.php');
      $this->SetFont('arialn','',10);
      $this->SetLeftMargin(5);
      $title = "INCOME STATEMENT";
      $page   = 'HAL';
      $title .= (GetParam("m","")=="13")?' (AUDIT)':"";
      $perush = (GetParam("office",""))?GetCompany (GetParam("office","")):'PT. AMPEL JAYA';

        $this->SetFont('arialn','',10);
      $this->Cell(200,5,$perush,0,1,'C',0);
      $this->Cell(200,5,$title,0,1,'C',0);
      $Periode = (GetParam("m","")=="13")?$this->BuildPeriod (GetParam("y",""), 12):$this->BuildPeriod (GetParam("y",""), GetParam("m",""));
      $this->Cell(200,5,"PERIODE: ".$Periode,0,1,'C',0);
      $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);

        //Column widths
        $this->SetFont('arialn','',10);
        $this->Cell($this->ColSize[0],7,"DESKRIPSI",1,0,'C',0);
        $this->Cell($this->ColSize[1],7,"S/D BLN LALU",1,0,'C',0);
        $this->Cell($this->ColSize[2],7,"MUTASI",1,0,'C',0);
        $this->Cell($this->ColSize[3],7,"S/D BLN INI",1,0,'C',0);
        $this->Cell($this->ColSize[4],7,"BLN KE-".GetParam("lm","")." THN ".GetParam("ly",""),1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
  }

  function BuildPeriod ($year, $month) {
    $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
    return strtoupper(date("d ", mktime(0, 0, 0, $month+1, 0, $year))).$arraymonth[$month].strtoupper(date(" Y", mktime(0, 0, 0, $month+1, 0, $year)));
  }

  function Footer() {
    $this->Cell(205,0,'','T');
        $this->Ln();
        $this->Cell(205,5,"  ( printed : ".date("F j, Y, g:i a")." )",0,1,'L',0);
  }

  function Period($Year, $Month, $LastYear, $LastMonth){
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) {$LMonth="0".($Month-1);}
    else $LMonth=$Month-1;
    if ($LastMonth<10) $LastMonth = "0".$LastMonth;
    $this->_CYear  = $Year;
    $this->_CMonth = $Month;
    $this->_SMonth = "aku".$Month;
    $this->_LastYear = $LastYear;
    $this->_LastMonth = "aku".$LastMonth;
    if ($Month==1) $this->_LSMonth = "awal";
    else $this->_LSMonth = "aku".$LMonth;
  }

  function DBConnection ($Conn) {
    $this->DBConnection = $Conn;
  }

  function GenerateReport() {
    $SQL = "SELECT * FROM rpt_income_statement order by placeorder";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
    if ($MyResults) {
      foreach ($MyResults as $RowResults){
        $this->PutRowInArray($RowResults["deskripsi"],$RowResults["skada"],$RowResults["istotal"],$RowResults["inverse"]);
      }
      $this->PutInLine();
    }
  }

  function PutRowInArray($description,$skada,$istotal,$inverse) {
		$this->TableName = "tblcoa";
      $countL = count($this->LeftTree);
      if (strlen($skada)) {
        $SkadaInRow = explode(",",$skada);
        for ($x=0;$x<count($SkadaInRow);$x++) {
           if (strlen($WhereSQL)) $WhereSQL .= " or a.skada like '{$SkadaInRow[$x]}%' ";
           else $WhereSQL   .= " a.skada like '{$SkadaInRow[$x]}%' ";
        }
        /*
        $SQL = "
          SELECT a.skada, b.{$this->_LastMonth} as awal,a.{$this->_SMonth} as currmonth,a.{$this->_LSMonth} as lastmonth
          FROM {$this->TableName} a
          LEFT JOIN {$this->TableName} b ON a.skada=b.skada AND b.ledger={$this->_LastYear}
          WHERE a.ledger='{$this->_CYear}' AND ($WhereSQL)
        ";
        */
        $SQL = "
          SELECT sum(b.{$this->_LastMonth}) as lastyearmonth,sum(a.{$this->_SMonth}) as currmonth,sum(a.{$this->_LSMonth}) as lastmonth
          FROM {$this->TableName} a
          LEFT JOIN {$this->TableName} b ON a.skada=b.skada AND b.ledger={$this->_LastYear}
          WHERE a.ledger='{$this->_CYear}' AND ($WhereSQL)
        ";
        //print $description."<br>\n".$SQL."<p>\n";
        $RowResult = $this->DBConnection->get_row($SQL);
        $this->LeftTree[$countL]["total_current"]   = $RowResult->currmonth;
        $this->LeftTree[$countL]["total_last"]      = $RowResult->lastmonth;
        $this->LeftTree[$countL]["total_mutasi"]    = $RowResult->currmonth-$RowResult->lastmonth;
        $this->LeftTree[$countL]["total_last_year"] = $RowResult->lastyearmonth;
      }
      $this->LeftTree[$countL]["deskripsi"] = $description;
      $this->LeftTree[$countL]["skada"]     = $skada;
      $this->LeftTree[$countL]["istotal"]   = $istotal;
      $this->LeftTree[$countL]["inverse"]   = $inverse;
  }

  function PutInLine() {
	$this->ColSize = array(85,30,30,30,30);
    $rowcount = count($this->LeftTree);
    //print_r($this->LeftTree);
    for ($x=0;$x<$rowcount;$x++) {
      if ($this->LeftTree[$x]["istotal"]==1) $LeftLine="TB";
      elseif ($this->LeftTree[$x]["istotal"]==2) {
        $DescLine="TB";
        $LeftLine="TB";
      } else {
        $DescLine="";
        $LeftLine="";
      }

      //if ($this->LeftTree[$x]["deskripsi"])
      $this->Cell($this->ColSize[0],4,$this->LeftTree[$x]["deskripsi"],$DescLine.'LR',0,'L',0);
      if (strlen($this->LeftTree[$x]["skada"])) {
        if ($this->LeftTree[$x]["inverse"]==1) {
          $this->LeftTree[$x]["total_current"]=$this->LeftTree[$x]["total_current"]*-1;
          $this->LeftTree[$x]["total_last"]=$this->LeftTree[$x]["total_last"]*-1;
          $this->LeftTree[$x]["total_mutasi"]=$this->LeftTree[$x]["total_mutasi"]*-1;
          $this->LeftTree[$x]["total_last_year"]=$this->LeftTree[$x]["total_last_year"]*-1;
        }
        $total_current   = ($this->LeftTree[$x]["total_current"]<0)?"(".number_format($this->LeftTree[$x]["total_current"]*-1).")":number_format($this->LeftTree[$x]["total_current"]);
        $total_last      = ($this->LeftTree[$x]["total_last"]<0)?"(".number_format($this->LeftTree[$x]["total_last"]*-1).")":number_format($this->LeftTree[$x]["total_last"]);
        $total_mutasi    = ($this->LeftTree[$x]["total_mutasi"]<0)?"(".number_format($this->LeftTree[$x]["total_mutasi"]*-1).")":number_format($this->LeftTree[$x]["total_mutasi"]);
        $total_last_year = ($this->LeftTree[$x]["total_last_year"]<0)?"(".number_format($this->LeftTree[$x]["total_last_year"]*-1).")":number_format($this->LeftTree[$x]["total_last_year"]);
        $this->Cell($this->ColSize[1],4,$total_last,$LeftLine.'LR',0,'R',0);
        $this->Cell($this->ColSize[2],4,$total_mutasi,$LeftLine.'LR',0,'R',0);
        $this->Cell($this->ColSize[3],4,$total_current,$LeftLine.'LR',0,'R',0);
        $this->Cell($this->ColSize[4],4,$total_last_year,$LeftLine.'LR',0,'R',0);
      } else {
        $this->Cell($this->ColSize[1],4,"",'LR',0,'R',0);
        $this->Cell($this->ColSize[2],4,"",'LR',0,'R',0);
        $this->Cell($this->ColSize[3],4,"",'LR',0,'R',0);
        $this->Cell($this->ColSize[4],4,"",'LR',0,'R',0);
      }
      $this->Ln();
    }
  }

}
?>
